<?php

/**
 * uniBoard Translate Language
 *
 * Light and fast forum solution for web 2.0-like communities.
 *
 * @package		uniBoard
 * @author		Rafał Pitoń
 * @license		http://www.gnu.org/licenses/gpl-3.0.html
 * @since		Version 0.1
 * 
 * ------------------------------------------------------------------------
 * 
 * $Date: 2010-09-15 17:35:38 +0000 (Wed, 15 Sep 2010) $
 * $Revision: 61 $
 * $Author: rafio.xudb@gmail.com $
 */

// ------------------------------------------------------------------------

class action_translate{
	
	/**
	 * Main class pointer
	 *
	 * @var object
	 */
	
	private $appCore		= NULL;
	
	/**
	 * Language API
	 *
	 * @var object
	 */
	
	private $api			= NULL;
	
	/**
	 * Language Files
	 *
	 * @var array
	 */
	
	private $files			= array();
	
	/**
	 * Language strings from loaded file
	 *
	 * @var array
	 */
	
	private $lang			= array();
	
	// ------------------------------------------------------------------------

	/**
	 * Initialise Action
	 *
	 * @param object $appCore
	 */
	
	function __construct( $appCore){
		
		// Store appCore pointer
		$this -> appCore = $appCore;
	
		// Start Language API
		$this -> api = new api_language( $appCore, $appCore -> _REQ( 'lang'));
		
		// Did we loaded api from DB?
		if ( $this -> api -> error || !$this -> api -> data_loaded)
		{
			// Draw last error
			$appCore -> output -> error( $appCore -> lang['acp_language_not_found'], $appCore -> lang['acp_languages_list']);
			
			// Trow user back to languages list
			new action_langs( $appCore);	
		}
		else
		{	
			// Load fields templates
			$appCore -> templates['form_inputs'] = new tpl_form_inputs( $this -> appCore);
			
			// Load lang files
			$this -> files = $appCore -> getLanguageFiles();
			
			// Cache output
			ob_start();
			
			// Draw File contents
			if ( $appCore -> _REQ( 'do') == 'show')
			{
				// Show file
				$this -> drawFileContent();
			}
			else
			{
				// Draw List of files
				$this -> drawFilesSelect();
			}
			
			// Draw output
			$appCore -> output -> draw( $appCore -> lang['language_op_translate'], array( $appCore -> adminLink( 'act=langs') => $appCore -> lang['acp_languages_list'],
			'' => htmlspecialchars( $this -> api -> language_name)
			), ob_get_clean());
		
		}
		
	}
	
	// ------------------------------------------------------------------------

	/**
	 * Draws files select
	 *
	 */
	
	function drawFilesSelect(){
		
		// Open Form
		echo $this -> appCore -> output -> openForm( $this -> appCore -> adminLink( 'act=translate&lang=' . $this -> api -> language_id . '&do=show'));		
		echo $this -> appCore -> output -> openFormBlock();
		
		// Draw Fields
		echo $this -> appCore -> output -> formItem( $this -> appCore -> lang['language_form_select_file'], $this -> appCore -> drawList( 'file', $this -> files, $this -> appCore -> _REQ( 'file')) . ' ' . $this -> appCore -> drawButton( '', $this -> appCore -> lang['go'], 'form_button_inline'));
		
		// Close Form
		echo $this -> appCore -> output -> closeFormBlock();
		echo $this -> appCore -> output -> closeForm();
		
	}
	
	// ------------------------------------------------------------------------

	/**
	 * Draws files select
	 *
	 */
	
	function drawFileContent(){
		
		// File to draw
		$file = $this -> appCore -> secureFilename( $this -> appCore -> _REQ( 'file'));
		
		// File exists?
		if ( !isset( $this -> files[$file]) || !file_exists( CACHE_PATH . 'lang_' . $this -> api -> language_id . '/' . $this -> files[$file]))
		{
			echo 'aaaa';
			// Set error
			$this -> appCore -> output -> error( $this -> appCore -> lang['acp_language_file_not_found'], htmlspecialchars( $this -> api -> language_name));
			
			// Draw List of files
			$this -> drawFilesSelect();
		}
		else
		{
			// Text File?
			if ( isset( $this -> files[$file]) && substr( $this -> files[$file], strpos( $this -> files[$file], '.')) == '.txt')
			{
				// Updating?
				if (  $this -> appCore -> _POST( 'update') == 1)
				{
					// Save file
					file_put_contents( CACHE_PATH . 'lang_' . $this -> api -> language_id . '/' . $this -> files[$file], $this -> appCore -> _POST('lang_file_content'));
					
					// Create Log
					$this -> appCore -> logAdmin( 'log_admin_edited_language_strings', htmlspecialchars( $this -> api -> language_name) . ' &raquo; ' . htmlspecialchars( $file));
				
					// Redirect back to file
					echo $this -> appCore -> output -> drawRedirect( $this -> appCore -> adminLink( 'act=translate&lang=' . $this -> api -> language_id . '&do=show&file=' . urlencode( $file)), $this -> appCore -> lang['acp_language_file_updated']);
					
					// Stop runtime there
					exit();
				}
				else
				{
					// Draw List of files
					$this -> drawFilesSelect();
							
					// Open Form
					echo $this -> appCore -> output -> openForm( $this -> appCore -> adminLink( 'act=translate&lang=' . $this -> api -> language_id . '&do=show&file=' . urlencode( $file)));
					echo $this -> appCore -> drawHidden( 'update', 1);
					echo $this -> appCore -> output -> openFormBlock( $file, $this -> appCore -> lang['language_form_file_template_contents']);	
					
					// Draw text-area
					echo $this -> appCore -> output -> tableRow( $this -> appCore -> drawBigTextArea( 'lang_file_content', file_get_contents( CACHE_PATH . 'lang_' . $this -> api -> language_id . '/' . $this -> files[$file])));

					// Draw button
					echo $this -> appCore -> output -> submitButton( $this -> appCore -> lang['save_changes']);
					
					// Close Form
					echo $this -> appCore -> output -> closeFormBlock();
					echo $this -> appCore -> output -> closeForm();
				}
			}
			else
			{
				// Include file
				include( CACHE_PATH . 'lang_' . $this -> api -> language_id . '/' . $this -> files[$file]);
				
				// File contains strings?
				if ( empty( $this -> lang))
				{
					// Set error
					$this -> appCore -> output -> error( $this -> appCore -> lang['acp_language_file_empty'], htmlspecialchars( $this -> api -> language_name));
				}
				else
				{
					// Updating?
					if (  $this -> appCore -> _POST( 'update') == 1)
					{
						// New file content
						$new_file_content = '';
						$file_update = false;
						
						// Reiterate strings
						foreach ( $this -> lang as $string_key => $sting_value)
						{
							// File key exists?
							if ( isset( $_POST['string_' . $string_key]))
							{
								// New value?
								$new_value = trim( $_POST['string_' . $string_key]);
								
								// Has content?
								if ( isset( $new_value[0]) && !is_array( $_POST['string_' . $string_key]))
								{
									// It has new content, put new value to lang string
									$this -> lang[$string_key] = $_POST['string_' . $string_key];
									
									// Put it to new file content
									$new_file_content .= '$this -> lang[' . var_export( $string_key, true) . '] = ' . var_export( $_POST['string_' . $string_key], true) . ';
	';
									// Tell function to update string
									$file_update = true;
								}
								else
								{
									// Put string as t is
									$new_file_content .= '$this -> lang[' . var_export( $string_key, true) . '] = ' . var_export( $sting_value, true) . ';
	';
								}
							}
							else
							{
								// Put string as t is
								$new_file_content .= '$this -> lang[' . var_export( $string_key, true) . '] = ' . var_export( $sting_value, true) . ';
	';
							}
						}
						
						// Update file?
						if ( $file_update)
						{
							// Save file
							file_put_contents( CACHE_PATH . 'lang_' . $this -> api -> language_id . '/' . $this -> files[$file], '<?php
	
	' . class_cache::makeFileStamp( 'LANGUAGE ' . $this -> api -> language_id . ' STRINGS') . $new_file_content);
							
							// Create Log
							$this -> appCore -> logAdmin( 'log_admin_edited_language_strings', htmlspecialchars( $this -> api -> language_name) . ' &raquo; ' . htmlspecialchars( $file));
						
							// Redirect back to file
							echo $this -> appCore -> output -> drawRedirect( $this -> appCore -> adminLink( 'act=translate&lang=' . $this -> api -> language_id . '&do=show&file=' . urlencode( $file)), $this -> appCore -> lang['acp_language_file_updated']);
						}
						else
						{
							// Just trow us back to file
							echo $this -> appCore -> output -> drawRedirect( $this -> appCore -> adminLink( 'act=translate&lang=' . $this -> api -> language_id . '&do=show&file=' . urlencode( $file)), $this -> appCore -> lang['acp_language_file_untouched']);
						}
						
						// Stop runtime there
						exit();
					}
					else
					{		
						// Draw List of files
						$this -> drawFilesSelect();
								
						// Open Form
						echo $this -> appCore -> output -> openForm( $this -> appCore -> adminLink( 'act=translate&lang=' . $this -> api -> language_id . '&do=show&file=' . urlencode( $file)));
						echo $this -> appCore -> drawHidden( 'update', 1);
						echo $this -> appCore -> output -> openFormBlock( $file, $this -> appCore -> lang['language_form_file_contents']);	
						
						// Reiterate strings
						foreach ( $this -> lang as $string_key => $sting_value)
						{
							// Draw string
							echo $this -> appCore -> output -> formItem( $string_key, $this -> appCore -> drawTextArea( 'string_' . $string_key, htmlspecialchars( $this -> appCore -> _POST( 'string_' . $string_key))), htmlspecialchars( $sting_value));
						}
						
						// Draw button
						echo $this -> appCore -> output -> submitButton( $this -> appCore -> lang['save_changes']);
						
						// Close Form
						echo $this -> appCore -> output -> closeFormBlock();
						echo $this -> appCore -> output -> closeForm();
					}
				}
			}
		}
		
	}
	
}